<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Support\Facades\DB;

class Supplier extends Authenticatable
{
    use Notifiable,HasRoles;

    protected $table = 'supplier_manage';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'company_name','contact', 'phone', 'address','phone','uuid'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function getSupplierProductsList($where,$search,$page,$limit)
    {

        $result =  DB::table('supplier_products')
            ->where(function($query) use($search) {
                if (!empty($search['product_name'])) {
                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
                }
            })
            ->where($where)
            ->select('*')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('supplier_products')
            ->where(function($query) use($search) {
                if (!empty($search['product_name'])) {
                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
                }
            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>$result,'count'=>$count];
        return self::convertObjectToArray($data);

    }



    public function getConsumableList($where,$search,$page,$limit)
    {

        $result =  DB::table('consumable')
            ->where(function($query) use($search) {
                if (!empty($search['product_name'])) {
                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
                }
            })
            ->where($where)
            ->select('*')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('consumable')
            ->where(function($query) use($search) {
                if (!empty($search['product_name'])) {
                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
                }
            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>$result,'count'=>$count];
        return self::convertObjectToArray($data);

    }


    public function materialApprovalData($where,$search,$page,$limit)
    {

        $result =  DB::table('s_products_approval')
            ->where(function($query) use($search) {
                if (!empty($search['product_name'])) {
                    $query->where('goodsname', 'like', '%' . $search['product_name'] . '%');
                }
            })
            ->where($where)
            ->select('*')
            ->orderBy('create_time','desc')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('s_products_approval')
            ->where(function($query) use($search) {
                if (!empty($search['product_name'])) {
                    $query->where('goodsname', 'like', '%' . $search['product_name'] . '%');
                }
            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>self::convertObjectToArray($result),'count'=>$count];
        return self::convertObjectToArray($data);

    }

    public function getOneProductApprovalData($table,$where)
    {
        $result = DB::table($table)
            ->select('*')
            ->where($where)
            ->first();
        return self::convertObjectToArray($result);
    }


    public function getOneProductData($table,$where)
    {
        $result = DB::table($table)
            ->select('*')
            ->where($where)
            ->first();
        return self::convertObjectToArray($result);
    }


    public function getProductData($table,$where)
    {

        $query = DB::table($table);
        foreach ($where as $field => $value) {
            if (is_array($value)) {
                $query->whereIn($field, $value);
            } else {
                $query->where($field, $value);
            }
        }
        $result =  $query->get()->toArray();
        return self::convertObjectToArray($result);
    }

    public function getOnePriceData($table,$where)
    {
        $result = DB::table($table)
            ->select('*')
            ->where($where)
            ->orderBy('create_time','desc')
            ->first();

        if($result){
            return self::convertObjectToArray($result);
        }else{
            return $result;
        }
    }

    public  function setProductsApproval($data)
    {

        unset($data['s']);
        //插入数据表
        return  DB::table('s_products_approval')->insert($data);

    }

    public  function updateProductsApproval($data)
    {
        unset($data['s']);
        //修改数据表
        return  DB::table('s_products_approval')->where('id',$data['id'])->update($data);
    }

    public  function updateHospitalRecord($data)
    {
        unset($data['s']);
        //修改数据表
        return  DB::table('s_hospital_record')->where('id',$data['id'])->update($data);
    }

    public function getData($table,$where,$field)
    {
        $result = DB::table($table)
            ->select($field)
            ->where($where)
            ->get()->toArray();
        return self::convertObjectToArray($result);
    }


    public function getOneBasicDictionaryData($table,$where,$field)
    {
        $result = DB::table($table)
            ->select($field)
            ->where($where)
            ->first();
        if($result){
            return  self::convertObjectToArray($result);
        }else{
            return $result;
        }
    }

    public function getGoodData($where)
    {
        $result =  DB::table('supplier_products as sp')
            ->join('consumable as c', 'c.product_code', '=', 'sp.product_code')
            ->where($where)
            ->select('*')
            ->get()->toArray();
        return self::convertObjectToArray($result);

    }



    public  function setOperationRecord($data)
    {
        return  DB::table('s_operation_record')->insert($data);

    }

    public  function setHospitalRecord($data)
    {
        return  DB::table('s_hospital_record')->insertGetId($data);
    }

    public static function getShopOneData($table,$where, $field = ['*'])
    {
        $result = DB::table($table)
            ->select($field)
            ->whereIn('product_code',$where)
            ->get()->toArray();
        return self::convertObjectToArray($result);
    }


    public function getOperationRecordList($where,$search,$page,$limit)
    {

        $result =  DB::table('s_operation_record')
//            ->where(function($query) use($search) {
//                if (!empty($search['operation_number'])) {
//                    $query->where('operation_number',$search['operation_number']);
//                }
//            })
            ->where($where)
            ->select('*')
            ->orderBy('create_time','desc')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('s_operation_record')
//            ->where(function($query) use($search) {
//                if (!empty($search['product_name'])) {
//                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
//                }
//            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>$result,'count'=>$count];
        return self::convertObjectToArray($data);

    }



    public function getDetailsDataList($where,$search,$page,$limit)
    {

        $result =  DB::table('v_rk_copy')
            ->where(function($query) use($search) {
                if (!empty($search['date_time'])) {
                    $query->where('date_time',$search['date_time']);
                }
                if (!empty($search['product_name'])) {
                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
                }
                if (!empty($search['product_code'])) {

                    $query->where('product_code',  $search['product_code']);
                }
            })
            ->where($where)
            ->select('*')
            ->orderBy('date_time','desc')
            ->offset($page)->limit($limit)
            ->get()->toArray();

        $count = DB::table('v_rk_copy')
            ->where(function($query) use($search) {
                if (!empty($search['date_time'])) {
                    $query->where('date_time',$search['date_time']);
                }
                if (!empty($search['product_name'])) {
                    $query->where('product_name', 'like', '%' . $search['product_name'] . '%');
                }
                if (!empty($search['product_code'])) {
                    $query->where('product_code',  $search['product_code']);
                }
            })
            ->select('id')
            ->where($where)
            ->count();

        $data = ['data'=>$result,'count'=>$count];
        return self::convertObjectToArray($data);

    }


    public function getfieldData($table,$where)
    {

        $result =  DB::table($table)
            ->where($where)
            ->select('*')
            ->orderBy('id','desc')
            ->get()->toArray();
        return self::convertObjectToArray($result);

    }




    public function getHangingNetData($where)
    {
        $result = DB::connection('website')->table('t_catalog_consumable_specmode')
            ->select('pubonlnPric')
            ->where($where)
            ->orderBy('captureTime', 'desc')
            ->first();
        if (!$result){
            return [];
        }
        return self::convertObjectToArray($result);
    }

    public function getHospitalRecordData($table,$where,$field)
    {
        $result = DB::table($table)
            ->select($field)
            ->where($where)
            ->first();
        if($result){
            return  self::convertObjectToArray($result);
        }else{
            return $result;
        }
    }


    public  function delRecord($table,$where)
    {
        return  DB::table('s_operation_record')->where($where)->delete();
    }

    public function getRkTotalList($where)
    {

        $result =  DB::table('v_rk_total')
            ->where(function($query) use($where) {
                    $query->where('tax_included_amount', '!=', 0);
            })
            ->where($where)
            ->select('*')
            ->get()->toArray();

        return self::convertObjectToArray($result);

    }

    protected static function convertObjectToArray($objects): array {
        if (is_array($objects)) {
            foreach ($objects as &$oneObject) {
                $oneObject = (array)$oneObject;
            }
        } elseif (is_object($objects)) {
            if ($objects instanceof Collection) {
                $objects = self::convertObjectToArray($objects->toArray());
            } else {
                $objects = (array)$objects;
            }
        }
        return $objects;
    }

}
